package com.huluobo.a2212mdmdemo.pages

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.lib_base.BaseTitle
import com.hyphenate.EMValueCallBack
import com.hyphenate.chat.EMClient
import com.hyphenate.chat.EMGroup
import com.hyphenate.chat.EMGroupManager.EMGroupStyle

import com.hyphenate.chat.EMGroupOptions
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking


/**
 *  Created by LC on 2025/6/11.
 */

@Composable
fun CreateGroupPage(navHostController: NavHostController) {
    //讨论组名字
    var groupName by remember {
        mutableStateOf("")
    }

    //讨论组描述
    var groupDesc by remember {
        mutableStateOf("")
    }

    Column(
        Modifier
            .fillMaxWidth()
            .padding(20.dp)
    ) {
        BaseTitle(title = "创建讨论组", navHostController = navHostController)
        Spacer(modifier = Modifier.height(10.dp))
        OutlinedTextField(value = groupName, onValueChange = { groupName = it }, Modifier.fillMaxWidth(), label = {
            Text(text = "讨论组名字")
        })
        OutlinedTextField(value = groupDesc, onValueChange = { groupDesc = it }, Modifier.fillMaxWidth(), label = {
            Text(text = "讨论组描述")
        }, maxLines = 3)

        Button(onClick = {
            //设置文件
            val option = EMGroupOptions()
            //讨论组最大人数
            option.maxUsers = 100
            //设置私有群或者公开群 **************注意*************,必须是EMGroupStylePublicOpenJoin 其他的形式不能直接加群,需要群组同意
            option.style = EMGroupStyle.EMGroupStylePublicOpenJoin
            EMClient.getInstance().groupManager()
                .asyncCreateGroup(
                    groupName,
                    groupDesc,
                    //数组中需要有自己
                    arrayOf(EMClient.getInstance().currentUser),
                    //邀请入群的消息,由于不需要邀请所以填空
                    "",
                    option,
                    object : EMValueCallBack<EMGroup> {
                        override fun onSuccess(value: EMGroup?) {
                            //注意一般callback中都是子线程的,需要使用协程切换到主线程
                            runBlocking(Dispatchers.Main) {
                                ToastUtils.showLong("创建讨论组成功")
                                navHostController.popBackStack()
                            }
                        }

                        override fun onError(error: Int, errorMsg: String?) {
                            ToastUtils.showLong("创建讨论组失败:$error,msg:$errorMsg")
                        }
                    })
        }) {
            Text(text = "创建讨论组")
        }
    }
}